home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Lib / posixpath.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  11.3 KB  |  321 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. '''Common pathname manipulations, Posix version. 
  5. Instead of importing this module
  6. directly, import os and refer to this module as os.path.
  7. '''
  8. import os
  9. import stat
  10.  
  11. def normcase(s):
  12.     '''Normalize case of pathname.  Has no effect under Posix'''
  13.     return s
  14.  
  15.  
  16. def isabs(s):
  17.     '''Test whether a path is absolute'''
  18.     return s[:1] == '/'
  19.  
  20.  
  21. def join(a, *p):
  22.     """Join two or more pathname components, inserting '/' as needed"""
  23.     path = a
  24.     for b in p:
  25.         if b[:1] == '/':
  26.             path = b
  27.         elif path == '' or path[-1:] == '/':
  28.             path = path + b
  29.         else:
  30.             path = path + '/' + b
  31.     
  32.     return path
  33.  
  34.  
  35. def split(p):
  36.     '''Split a pathname.  Returns tuple "(head, tail)" where "tail" is 
  37. everything after the final slash.  Either part may be empty'''
  38.     import string
  39.     i = string.rfind(p, '/') + 1
  40.     (head, tail) = (p[:i], p[i:])
  41.     if head and head != '/' * len(head):
  42.         while head[-1] == '/':
  43.             head = head[:-1]
  44.     
  45.     return (head, tail)
  46.  
  47.  
  48. def splitext(p):
  49.     '''Split the extension from a pathname.  Extension is everything from the
  50. last dot to the end.  Returns "(root, ext)", either part may be empty'''
  51.     (root, ext) = ('', '')
  52.     for c in p:
  53.         if c == '/':
  54.             (root, ext) = (root + ext + c, '')
  55.         elif c == '.':
  56.             if ext:
  57.                 (root, ext) = (root + ext, c)
  58.             else:
  59.                 ext = c
  60.         elif ext:
  61.             ext = ext + c
  62.         else:
  63.             root = root + c
  64.     
  65.     return (root, ext)
  66.  
  67.  
  68. def splitdrive(p):
  69.     '''Split a pathname into drive and path. On Posix, drive is always 
  70. empty'''
  71.     return ('', p)
  72.  
  73.  
  74. def basename(p):
  75.     '''Returns the final component of a pathname'''
  76.     return split(p)[1]
  77.  
  78.  
  79. def dirname(p):
  80.     '''Returns the directory component of a pathname'''
  81.     return split(p)[0]
  82.  
  83.  
  84. def commonprefix(m):
  85.     '''Given a list of pathnames, returns the longest common leading component'''
  86.     if not m:
  87.         return ''
  88.     
  89.     prefix = m[0]
  90.     for item in m:
  91.         for i in range(len(prefix)):
  92.             pass
  93.         
  94.     
  95.     return prefix
  96.  
  97.  
  98. def getsize(filename):
  99.     '''Return the size of a file, reported by os.stat().'''
  100.     st = os.stat(filename)
  101.     return st[stat.ST_SIZE]
  102.  
  103.  
  104. def getmtime(filename):
  105.     '''Return the last modification time of a file, reported by os.stat().'''
  106.     st = os.stat(filename)
  107.     return st[stat.ST_MTIME]
  108.  
  109.  
  110. def getatime(filename):
  111.     '''Return the last access time of a file, reported by os.stat().'''
  112.     st = os.stat(filename)
  113.     return st[stat.ST_MTIME]
  114.  
  115.  
  116. def islink(path):
  117.     '''Test whether a path is a symbolic link'''
  118.     
  119.     try:
  120.         st = os.lstat(path)
  121.     except (os.error, AttributeError):
  122.         return 0
  123.  
  124.     return stat.S_ISLNK(st[stat.ST_MODE])
  125.  
  126.  
  127. def exists(path):
  128.     '''Test whether a path exists.  Returns false for broken symbolic links'''
  129.     
  130.     try:
  131.         st = os.stat(path)
  132.     except os.error:
  133.         return 0
  134.  
  135.     return 1
  136.  
  137.  
  138. def isdir(path):
  139.     '''Test whether a path is a directory'''
  140.     
  141.     try:
  142.         st = os.stat(path)
  143.     except os.error:
  144.         return 0
  145.  
  146.     return stat.S_ISDIR(st[stat.ST_MODE])
  147.  
  148.  
  149. def isfile(path):
  150.     '''Test whether a path is a regular file'''
  151.     
  152.     try:
  153.         st = os.stat(path)
  154.     except os.error:
  155.         return 0
  156.  
  157.     return stat.S_ISREG(st[stat.ST_MODE])
  158.  
  159.  
  160. def samefile(f1, f2):
  161.     '''Test whether two pathnames reference the same actual file'''
  162.     s1 = os.stat(f1)
  163.     s2 = os.stat(f2)
  164.     return samestat(s1, s2)
  165.  
  166.  
  167. def sameopenfile(fp1, fp2):
  168.     '''Test whether two open file objects reference the same file'''
  169.     s1 = os.fstat(fp1)
  170.     s2 = os.fstat(fp2)
  171.     return samestat(s1, s2)
  172.  
  173.  
  174. def samestat(s1, s2):
  175.     '''Test whether two stat buffers reference the same file'''
  176.     if s1[stat.ST_INO] == s2[stat.ST_INO]:
  177.         pass
  178.     return s1[stat.ST_DEV] == s2[stat.ST_DEV]
  179.  
  180.  
  181. def ismount(path):
  182.     '''Test whether a path is a mount point'''
  183.     
  184.     try:
  185.         s1 = os.stat(path)
  186.         s2 = os.stat(join(path, '..'))
  187.     except os.error:
  188.         return 0
  189.  
  190.     dev1 = s1[stat.ST_DEV]
  191.     dev2 = s2[stat.ST_DEV]
  192.     if dev1 != dev2:
  193.         return 1
  194.     
  195.     ino1 = s1[stat.ST_INO]
  196.     ino2 = s2[stat.ST_INO]
  197.     if ino1 == ino2:
  198.         return 1
  199.     
  200.     return 0
  201.  
  202.  
  203. def walk(top, func, arg):
  204.     '''walk(top,func,args) calls func(arg, d, files) for each directory "d" 
  205. in the tree  rooted at "top" (including "top" itself).  "files" is a list
  206. of all the files and subdirs in directory "d".
  207. '''
  208.     
  209.     try:
  210.         names = os.listdir(top)
  211.     except os.error:
  212.         return None
  213.  
  214.     func(arg, top, names)
  215.     exceptions = ('.', '..')
  216.     for name in names:
  217.         if name not in exceptions:
  218.             name = join(top, name)
  219.             if isdir(name) and not islink(name):
  220.                 walk(name, func, arg)
  221.             
  222.         
  223.     
  224.  
  225.  
  226. def expanduser(path):
  227.     '''Expand ~ and ~user constructions.  If user or $HOME is unknown, 
  228. do nothing'''
  229.     if path[:1] != '~':
  230.         return path
  231.     
  232.     (i, n) = (1, len(path))
  233.     while i < n and path[i] != '/':
  234.         i = i + 1
  235.     if i == 1:
  236.         if not os.environ.has_key('HOME'):
  237.             return path
  238.         
  239.         userhome = os.environ['HOME']
  240.     else:
  241.         import pwd
  242.         
  243.         try:
  244.             pwent = pwd.getpwnam(path[1:i])
  245.         except KeyError:
  246.             return path
  247.  
  248.         userhome = pwent[5]
  249.     if userhome[-1:] == '/':
  250.         i = i + 1
  251.     
  252.     return userhome + path[i:]
  253.  
  254. _varprog = None
  255.  
  256. def expandvars(path):
  257.     '''Expand shell variables of form $var and ${var}.  Unknown variables
  258. are left unchanged'''
  259.     global _varprog
  260.     if '$' not in path:
  261.         return path
  262.     
  263.     if not _varprog:
  264.         import re
  265.         _varprog = re.compile('\\$(\\w+|\\{[^}]*\\})')
  266.     
  267.     i = 0
  268.     while 1:
  269.         m = _varprog.search(path, i)
  270.         if not m:
  271.             break
  272.         
  273.         (i, j) = m.span(0)
  274.         name = m.group(1)
  275.         if name[:1] == '{' and name[-1:] == '}':
  276.             name = name[1:-1]
  277.         
  278.         if os.environ.has_key(name):
  279.             tail = path[j:]
  280.             path = path[:i] + os.environ[name]
  281.             i = len(path)
  282.             path = path + tail
  283.         else:
  284.             i = j
  285.     return path
  286.  
  287.  
  288. def normpath(path):
  289.     '''Normalize path, eliminating double slashes, etc.'''
  290.     import string
  291.     slashes = ''
  292.     while path[:1] == '/':
  293.         slashes = slashes + '/'
  294.         path = path[1:]
  295.     comps = string.splitfields(path, '/')
  296.     i = 0
  297.     while i < len(comps):
  298.         if comps[i] == '.':
  299.             del comps[i]
  300.             while i < len(comps) and comps[i] == '':
  301.                 del comps[i]
  302.         elif comps[i] == '..' and i > 0 and comps[i - 1] not in ('', '..'):
  303.             del comps[i - 1:i + 1]
  304.             i = i - 1
  305.         elif comps[i] == '' and i > 0 and comps[i - 1] != '':
  306.             del comps[i]
  307.         else:
  308.             i = i + 1
  309.     if not comps and not slashes:
  310.         comps.append('.')
  311.     
  312.     return slashes + string.joinfields(comps, '/')
  313.  
  314.  
  315. def abspath(path):
  316.     if not isabs(path):
  317.         path = join(os.getcwd(), path)
  318.     
  319.     return normpath(path)
  320.  
  321.